<template>
    <main>
        <navBar :navTitle="$t('Zhuye')" showRight :showLeft="true" showLeftLan />
        <div class="full-width">
            <van-search v-model="searchVal" :placeholder="$t('Mingchengdaima')" @search="fnSearch" class="index-search" />

            <!--加权指数-->
            <van-row class="index-box">
                <!-- @click="fnIndex(item.type)" -->
                <van-col span="8" v-for="(item, index) in indexArr" :key="index">
                    <div>{{ item.name }}</div>
                    <i class="iconfont icon-xiangshang1 color-blueLight"></i>
                    <ul :class="item[56] > 0 ? 'color-green' : 'color-red'">
                        <li class="index-num">{{ item[6] }}</li>
                        <li><span v-if="item[56] > 0">+</span>{{ item[56] }}%</li>
                        <li>{{ item[11] }}</li>
                    </ul>

                </van-col>
            </van-row>

            <!--四大导航按钮-->
            <van-row class="index-btn-box" gutter="8">
                <van-col span="6" v-for="(item, index) in btnIndex4" :key="index">
                    <div class="btn-list" :class="'index-btn' + (index + 1)" @click="fnNavGo(index)">
                        <span class="txt-menu">{{ item.name }}</span>
                        <van-image :src="require('@/assets/images/index_' + (index + 1) + '.png')" />
                    </div>
                </van-col>
            </van-row>

            <!--实时股票咨询-->
            <ul class="news-box">
                <van-image :src="require('@/assets/images/index_news.png')" class="img-index-news" />
                <span class="news">{{$t('Zuixinxiaoxi')}}</span>
                <li v-for="(item, index) in newsArr" :key="item.id" class="news-list" @click="btnGoNewsInfo(item)">
                    <van-image class="img-news" :src="item.thumb" fit="fill" />
                    <div class="news-content">
                        <h4 class="ellipsis2 news-title">{{ item.title }}</h4>
                        <div class="bg-theme">
                            <van-icon name="underway-o" />
                            <span>{{ item.tranTime }}</span>
                        </div>
                    </div>
                </li>
            </ul>

        </div>
    </main>
</template>

<script>
import { index_dating_api, quser_sel_api, index_articlelist_api } from "@/assets/js/api";
export default {
    name: "index",
    data() {
        return {
            time_interval: "",
            searchVal: "",
            indexArr: [
                {
                    "6": 0,
                    "11": 0,
                    "56": 0,
                    "type": "VN",
                    "name": "VN-Index",
                },
                {
                    "6": 0,
                    "11": 0.65,
                    "56": 0,
                    "type": "HNX",
                    "name": "HNX-Index",
                },
                {
                    "6": 0,
                    "11": 0,
                    "56": 0,
                    "type": "UPCOM",
                    "name": "UPCOM-Index",
                }
            ],
            btnIndex4: [],

            /*新闻列表*/
            pageNo: 1,
            newsArr: [],
        };
    },
    methods: {

        /*大厅指数*/
        fnGetIndex() {
            index_dating_api({
                page: 1,
            }).then(res => {
                if (res.code == 200) {
                    this.index_obj = res.data;
                    const data_arr = res.data;
                    this.indexArr = [
                        {
                            name: "VN-Index",
                            ...data_arr.jiaquan,

                        }, {
                            name: "HNX-Index",
                            ...data_arr.guimai,
                        }, {
                            name: "UPCOM-Index",
                            ...data_arr.taizhi,
                        }
                    ]
                }
            })
        },
        fnSearch(val) {
            console.log(val);
            this.$router.push({
                name: "market_addStock",
                query: {
                    searchVal: val,
                }
            })
        },

        fnIndex(type) {
            this.$router.push({
                name: "market_index",
                query: {
                    type: type,
                }
            })
        },

        /*四大導航 跳轉*/
        fnNavGo(index) {
            let market_tab_active = index + 1;
            console.log(market_tab_active);
            if (index == 1) {
                market_tab_active = 3
            }
            if ([0, 1, 3].includes(index)) {
                this.$router.push({
                    name: "market",
                })

                localStorage.setItem("market_tab_active", market_tab_active);
            }

            /*licai*/
            if (index == 2) {
                this.$router.push({
                    name: "trade",
                })
            }
        },

        /*新闻詳情*/
        btnGoNewsInfo(item) {

            this.$router.push({
                name: "index_newsInfo",
                query: {
                    id: item.id,
                }
            })
        },

        /*获取新闻列表*/
        fnGetNews() {
            index_articlelist_api({
                page: this.pageNo,
            }).then(res => {
                if (res.code == 200) {
                    this.newsArr = res.data;
                }
            })
        },

    },

    created() {
        this.btnIndex4 = [
            {
                name: this.$t('Shichang'),
                path: "",
            }, {
                name: this.$t('Zhuce'),
                path: "",
            }, {
                name: this.$t('Caiwu'),
                path: "",
            }, {
                name: this.$t('Zichanleibie'),
                path: "",
            },
        ]
        this.fnGetNews();
        this.fnGetIndex();

        this.time_interval = setInterval(() => {
            // console.log("fnGetIndex");
            this.fnGetIndex();
        }, 5000)
    },
    destroyed() {
        clearInterval(this.time_interval);
    },
};
</script>
<style lang="scss" scoped>
.full-width {
    padding-bottom: 50px
}

.index-box {
    text-align: center;
    margin: 20px 0;

    .index-num {
        font-size: 16px;
        font-weight: bold;
    }
}

.index-btn-box {
    .btn-list {
        //background-color: #3365e3;
        color: #fff;
        border-radius: 6px;
        //height: 60px;
        //padding: 6px;
        font-size: 14px;
    }

    //.index-btn1{
    //    background: url("~@/assets/images/index_1.png") no-repeat;
    //    background-size: 100% 100%;
    //}
    //.index-btn2{
    //    background: url("~@/assets/images/index_2.png") no-repeat;
    //    background-size: 100% 100%;
    //}
    //.index-btn3{
    //    background: url("~@/assets/images/index_3.png") no-repeat;
    //    background-size: 100% 100%;
    //}
    //.index-btn4{
    //    background: url("~@/assets/images/index_4.png") no-repeat;
    //    background-size: 100% 100%;
    //}

}

.img-index-news {
    width: 30%;
    margin-bottom: 4px;
}

.news-box {
    margin-top: 12px;
    //padding-top: 20px;
    //height: calc( 100vh - 50px);
    //border: solid 1px red;
    //overflow: auto;
}

.news-list {
    display: flex;
    margin-bottom: 20px;

    .img-news {
        width: 36%;

        //flex: 1;
        /deep/.van-image__img {
            border-radius: 4px;
        }
    }

    .news-content {
        //flex: 2;
        width: 64%;
        padding-left: 10px;
    }

    .news-title {
        margin-bottom: 0;
        font-weight: bold;
    }

    .news-time {
        font-size: 12px;
        padding: 2px 3px;
        border-radius: 4px;
        background-color: #8ba6e6;
        color: #3365e3;
    }
}

.news-title {
    max-height: 50px;
}

.txt-menu {
    position: absolute;
    z-index: 99;
    padding-left: 5px;
    margin-top: 5px;
    font-size: 12px;
    width: 20%;
}

.news {
    position: absolute;
    left: 33px;
    font-size: 1.3rem;
    font-weight: bold;

    color: #3d6ce4
}

/deep/ .icon-kefu1 {
    font-size: 30px !important;
}
</style>